package com.example.demo.controller;


import com.example.demo.model.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.util.JsonData;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private UserMapper userMapper;


    //登录接口
    @PostMapping("/login")
    public JsonData login(HttpServletRequest request, @RequestBody User user) {
        //1、将页面提交的密码password进行md5加密处理
        System.out.println("user_id"+ user.getUser_id());
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
//        System.out.println("123456 md5="+DigestUtils.md5DigestAsHex("123456".getBytes()));
        User loginUser=userMapper.findUser(user.getUser_id());
        if(loginUser==null){
            return JsonData.buildError("用户不存在");
        }
        else {
            if(loginUser.getPassword().equals(md5Password)){
                return JsonData.buildSuccess(loginUser);
            }
            else{
                return JsonData.buildError("密码错误");
            }
        }


    }
}
